package sparqlclient;

import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import com.jbob.collection.Query;

public class SparqlClientHelper {

	public static void reformulationTerms(Query q){
		// synonymes
		for (String term : q.getWordsToSeek()) {
			for(String synonym : getSynonyms(term)) {
				q.addSysnonymToSeek(synonym);
			}
			for(String parent : getParents(term)) {
				q.addParentToSeek(parent);
			}
			for(String subClass : getSubClass(term)) {
				q.addSubClassToSeek(subClass);
			}
		}	
	}
	
	
    /**
     * @param args the command line arguments
     */
    private static ArrayList<String> getSynonyms(String term) {
    	ArrayList<String> synonyms = new ArrayList<String>();
    	
        SparqlClient sparqlClient = new SparqlClient("localhost:3030/space");
        
        String query = "ASK WHERE { ?s ?p ?o }";
        boolean serverIsUp = sparqlClient.ask(query);
        
         query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
         		"" +
         		"SELECT ?label ?synonym\n" +
        		"WHERE {\n" +
        		"	 ?x rdfs:label ?label.\n" +
        		"	 ?x rdfs:label ?synonym.\n" +
        		"	FILTER regex(?label, \"" + term + "\")" +
        		"}";
        
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        for (Map<String, String> result : results) {
            synonyms.add(result.get("synonym"));
        }
        
        return synonyms;
    }
    
    /**
     * @param args the command line arguments
     */
    private static ArrayList<String> getParents(String term) {
    	ArrayList<String> parent = new ArrayList<String>();
    	
        SparqlClient sparqlClient = new SparqlClient("localhost:3030/space");
        
        String query = "ASK WHERE { ?s ?p ?o }";
        boolean serverIsUp = sparqlClient.ask(query);
         query = "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
         		"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
         		"PREFIX fn:  <http://www.w3.org/2005/xpath-functions#>" +
         		"" +
         		"SELECT ?class ?labelClass\n" +
        		"WHERE {\n" +
        		"	 ?element rdfs:label ?label.\n" +
        		"	 ?element  rdf:type ?class.\n" +
        		"    ?class rdfs:label ?labelClass.\n" +
        		"	FILTER regex(?label, \'" + term + "\')\n" +
        		"}\n" +
        		"LIMIT 1\n";
        
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        for (Map<String, String> result : results) {
        	parent.add(result.get("parent"));
        }
        return parent;
    }
    
    /**
     * @param args the command line arguments
     */
    private static ArrayList<String> getSubClass(String term) {
    	ArrayList<String> subClass = new ArrayList<String>();
    	
        SparqlClient sparqlClient = new SparqlClient("localhost:3030/space");
        
        String query = "ASK WHERE { ?s ?p ?o }";
        boolean serverIsUp = sparqlClient.ask(query);
        
         query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
         		"" +
         		"SELECT ?classLabel ?subClassLabel\n" +
        		"WHERE {\n" +
        		"	 ?class rdfs:label ?classLabel.\n" +
        		"	 ?subClass rdfs:subClassOf ?class.\n" +
        		"    ?subClass rdfs:label ?subClassLabel.\n" +
        		"	FILTER regex(?classLabel, \'" + term + "\')\n" +
        		"}\n";
        
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        for (Map<String, String> result : results) {
        	subClass.add(result.get("subClassLabel"));
        }
        
        return subClass;
    }
    
}
